<?php

	class Walk_proxy extends Model
	{
		public function __construct()
		{
			parent::__construct();
		}
						
		public function getCountryList()
		{
			$country_list = array();

			$tmp = $this->db->query('
								SELECT
								code,
								name
								FROM country_list
							');
							 
			$numres = $tmp->num_rows();
			
			if( $numres > 0)
			{
				foreach ($tmp->result() as $countryRow)
				{				
					$country_list[$countryRow->code] = $countryRow->name;
				}
			}
			
			return $country_list;						
		}
		
		public function insertCMBRegistration( $hhId, $cmbCenterId, $regAmount, $curyear, $primarysession, $memberInfo )
		{
			$memberInfo = Member_info_value::cast($memberInfo);
			$prevyear = $curyear - 1;
			$na = "na";
			
			//Check if there is a home_url from the previous year and if so use it for the current year
			$tmp = $this->db->query('
								SELECT			IFNULL(crh.home_url,' . $this->db->escape($na) . ') AS home_url
								FROM			cmb_reg_history crh
								WHERE			crh.household_id_fk = ' . $this->db->escape($hhId) . ' AND
												crh.year = ' . $this->db->escape($prevyear) . ' AND
												crh.cmb_center_id_fk = ' . $this->db->escape($cmbCenterId) . '
							 ');
							 
			if( $tmp->num_rows() > 0)
			{
				if ($tmp->row()->home_url != "na")
					$urlKey = $tmp->row()->home_url;
				else
					$urlKey = uniqid();
			}
			else
				$urlKey = uniqid();

			if ($memberInfo->getLifeMember() == "yes")
			{
				$mType = "Life";
				$actAmount = 0.0;
			}
			else
			{
				$mType = "Regular";
				$actAmount = $regAmount;
			}
				
			$source = "unknown";
			$str_hash = md5($this->encrypt->get_random_password(32,32));
			
			$this->db->query('
								INSERT INTO			cmb_reg_history
													(
														household_id_fk,
														cmb_center_id_fk,
														amount_paid,
														registration_amount,
														fully_paid,
														enabled,
														type,
														source,
														session,
														year,
														donation_amount,
														payment_type,
														spousesession,
														home_url,
														display_name,
														date_modified,
														date_created,
														str_hash
													)
								VALUES				(
														' . $this->db->escape($hhId)  . ',
														' . $this->db->escape($cmbCenterId)  . ',
														0.0,
														' . $this->db->escape($actAmount)  . ',
														0,
														1,
														' . $this->db->escape($mType)  . ',
														' . $this->db->escape($source)  . ',
														' . $this->db->escape($primarysession)  . ',
														' . $this->db->escape($curyear)  . ',
														' . $this->db->escape($memberInfo->getDonation())  . ',
														' . $this->db->escape('unknown')  . ',
														' . $this->db->escape($memberInfo->getSpouseAttendSatsang())  . ',
														' . $this->db->escape($urlKey) . ',
														' . $this->db->escape($memberInfo->getDisplayName())  . ',
														Now(),
														Now(),
														' . $this->db->escape($str_hash)  . '														
													)
							 ');
			
			$cmbRegHistoryId = $this->db->query('
													SELECT 		cmb_reg_history_id 
													FROM 		cmb_reg_history
													WHERE		str_hash = ' . $this->db->escape($str_hash) . '
												  ');

			if( $cmbRegHistoryId->num_rows() == 1 )
			{
				$cmbRegHistoryId = $cmbRegHistoryId->row()->cmb_reg_history_id;
			}
			else
				$cmbRegHistoryId = 0;

			//$cmbRegHistoryId = getMaxDbId( 'cmb_reg_history_id', 'cmb_reg_history' );
			
			return $cmbRegHistoryId;
		}
		
		public function getTeacherGroupID($cmb_center_id, $year, $session, $grade)
		{
			$tmp = $this->db->query('
				SELECT tg.teacher_group_id
				FROM teacher_group tg
				WHERE
					tg.enabled = 1
					AND tg.year = ' . $this->db->escape($year)  . '
					AND tg.grade = ' . $this->db->escape($grade)  . '
					AND tg.session = ' . $this->db->escape($session)  . '
					AND tg.cmb_center_id_fk = ' . $this->db->escape($cmb_center_id)  . '
				');						 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->teacher_group_id;
			else
				return 0;			
		}

        public function getCenterID($centername)
        {
            $tmp = $this->db->query('
                SELECT cmb_center_id
                FROM cmb_center
                WHERE
                    center_name = ' . $this->db->escape($centername)  . '
                ');                      
            if( $tmp->num_rows() > 0)
                return $tmp->row()->cmb_center_id;
            else
                return 0;           
        }

		//Get current Balavihar week
		public function getBalaviharWeek( $curyear )
		{
			
			$tmp = $this->db->query('
								SELECT bw.bv_week_id,
										bw.seq,
										DATE_FORMAT(bw.bow_date,' . $this->db->escape("%d-%b-%Y") . ') AS bowdate
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
									AND bw.bow_date <= NOW()
								ORDER BY bw.bow_date DESC
								LIMIT 1;
							');
							 
			$bvweekarr = array();
			
			if( $tmp->num_rows() > 0)
			{
				array_push($bvweekarr, $tmp->row()->bv_week_id);
				array_push($bvweekarr, $tmp->row()->seq);
				array_push($bvweekarr, $tmp->row()->bowdate);
				
				return $bvweekarr;				
			}
			else
			{
				$tmp = $this->db->query('
									SELECT bw.bv_week_id,
											bw.seq,
											DATE_FORMAT(bw.bow_date,' . $this->db->escape("%d-%b-%Y") . ') AS bowdate
									FROM bv_week bw
									WHERE
										bw.seq = 1
									LIMIT 1;
								');

				array_push($bvweekarr, $tmp->row()->bv_week_id);
				array_push($bvweekarr, $tmp->row()->seq);
				array_push($bvweekarr, $tmp->row()->bowdate);
				
				return $bvweekarr;
			}
		}

		//Get current Balavihar week
		public function getBalaviharWeekDate( $curyear, $seq )
		{
			
			$tmp = $this->db->query('
								SELECT
									DATE_FORMAT(bw.bow_date,' . $this->db->escape("%d-%b-%Y") . ') AS bowdate
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
									AND bw.seq = ' . $this->db->escape($seq) . '
							');
							 
			if( $tmp->num_rows() > 0)
			{				
				return $tmp->row()->bowdate;				
			}
			else
				return "";
		}

		//Get all Balavihar week dates before Now
		public function getBalaviharAttendanceWeek( $curyear  )
		{
			$weekarr = array();
						
			$tmp = $this->db->query('
								SELECT bw.seq,
									   DATE_FORMAT(bw.bow_date,' . $this->db->escape("%d-%b-%Y") . ') AS bowdate
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
									AND bw.bow_date <= NOW()
								ORDER BY bw.seq ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			$weekidarr = array();
			if( $numweeks > 0)
			{				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekarr, $wkRow->seq.'=>'.$wkRow->bowdate);
				}
			}
			else
			{
				$tmp = $this->db->query('
									SELECT bw.seq,
										   DATE_FORMAT(bw.bow_date,' . $this->db->escape("%d-%b-%Y") . ') AS bowdate
									FROM bv_week bw
									WHERE
										bw.year = ' . $this->db->escape($curyear) . '
										AND bw.seq = 1;
								');				

				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekarr, $wkRow->seq.'=>'.$wkRow->bowdate);
				}
			}
						
			return $weekarr;			
		}
		
		//Get Balavihar attendance for specified class and week
		public function getBalaviharAttendance( $curyear, $centername, $session, $grade, $seq )
		{
			$bvattendance = array();
			$arridx = array();

			if ($grade != "Pre-K")
			{
				if ($grade == "K")
					$prevgrade = "Pre-K";
				else
					$prevgrade = strval(intval($grade)-1);
				
				//Get all entries from previous year
				/* $tmp = $this->db->query('
									SELECT
									bvax.bv_attend_xref_id,
									bvax.person_id_fk,
									p.first_name,
									p.last_name,
									bvax.status
									FROM bv_attend_xref bvax
									INNER JOIN bv_reg_history brh ON brh.person_id_fk=bvax.person_id_fk
									INNER JOIN person p ON p.person_id=brh.person_id_fk
									INNER JOIN bv_week bvw ON bvw.bv_week_id=bvax.bv_week_id_fk
									INNER JOIN cmb_center cc ON cc.cmb_center_id=brh.cmb_center_id_fk
									WHERE
									brh.year = ' . $this->db->escape($curyear-1) . '
									AND bvw.seq = ' . $this->db->escape($seq) . '
									AND bvw.year = ' . $this->db->escape($curyear) . '
									AND cc.center_name = ' . $this->db->escape($centername) . '
									AND brh.session = ' . $this->db->escape($session) . '
									AND brh.grade = ' . $this->db->escape($prevgrade) . '
									ORDER BY p.last_name ASC
								');
								 
				$numres = $tmp->num_rows();
				if( $numres > 0)
				{
					foreach ($tmp->result() as $wkRow)
					{				
						$bvattendancerec = array();
					
						array_push($bvattendancerec, $wkRow->bv_attend_xref_id);
						array_push($bvattendancerec, $wkRow->person_id_fk);
						array_push($bvattendancerec, $wkRow->first_name);
						array_push($bvattendancerec, $wkRow->last_name);
						array_push($bvattendancerec, $wkRow->status);
	
						array_push($bvattendance, $bvattendancerec);
						array_push($arridx,strtolower($wkRow->last_name));
					}
				} */
			}
			else
				$prevgrade = $grade;

			//Get all entries for current year
			if ($grade != "Pre-K")
			{
				$tmp = $this->db->query('
									SELECT
									bvax.bv_attend_xref_id,
									bvax.person_id_fk,
									p.first_name,
									p.last_name,
									bvax.status
									FROM bv_attend_xref bvax
									INNER JOIN bv_reg_history brh ON brh.person_id_fk=bvax.person_id_fk
									INNER JOIN person p ON p.person_id=brh.person_id_fk
									INNER JOIN bv_week bvw ON bvw.bv_week_id=bvax.bv_week_id_fk
									INNER JOIN cmb_center cc ON cc.cmb_center_id=brh.cmb_center_id_fk
									WHERE
									brh.year = ' . $this->db->escape($curyear) . '
									AND bvw.seq = ' . $this->db->escape($seq) . '
									AND bvw.year = ' . $this->db->escape($curyear) . '
									AND cc.center_name = ' . $this->db->escape($centername) . '
									AND brh.session = ' . $this->db->escape($session) . '
									AND brh.grade = ' . $this->db->escape($grade) . '
                                    AND p.cmb_member = 1
                                    AND p.enabled = 1
                                    AND brh.enabled = 1
                                    GROUP BY p.person_id
									ORDER BY brh.session ASC, p.last_name ASC;
								');
			}
			else
			{				
				$tmp = $this->db->query('
									SELECT
									bvax.bv_attend_xref_id,
									bvax.person_id_fk,
									p.first_name,
									p.last_name,
									bvax.status
									FROM bv_attend_xref bvax
									INNER JOIN bv_reg_history brh ON brh.person_id_fk=bvax.person_id_fk
									INNER JOIN person p ON p.person_id=brh.person_id_fk
									INNER JOIN bv_week bvw ON bvw.bv_week_id=bvax.bv_week_id_fk
									INNER JOIN cmb_center cc ON cc.cmb_center_id=brh.cmb_center_id_fk
									WHERE
									brh.year = ' . $this->db->escape($curyear) . '
									AND bvw.seq = ' . $this->db->escape($seq) . '
									AND bvw.year = ' . $this->db->escape($curyear) . '
									AND cc.center_name = ' . $this->db->escape($centername) . '
									AND brh.session = ' . $this->db->escape($session) . '
									AND brh.grade = ' . $this->db->escape($grade) . '
                                    AND p.cmb_member = 1
                                    AND p.enabled = 1
                                    AND brh.enabled = 1
                                    GROUP BY p.person_id
									ORDER BY brh.session ASC, p.last_name ASC;
								');
			}
							 
			$numres = $tmp->num_rows();
			
			if( $numres > 0)
			{
				foreach ($tmp->result() as $wkRow)
				{				
					$bvattendancerecnew = array();
				
					array_push($bvattendancerecnew, $wkRow->bv_attend_xref_id);
					array_push($bvattendancerecnew, $wkRow->person_id_fk);
					array_push($bvattendancerecnew, $wkRow->first_name);
					array_push($bvattendancerecnew, $wkRow->last_name);
					array_push($bvattendancerecnew, $wkRow->status);

					array_push($bvattendance, $bvattendancerecnew);
					array_push($arridx,strtolower($wkRow->last_name));
				}				
			}
						
			//Sort by last_name
			asort($arridx);
			
			$bvattendancesrt = array();
			foreach ($arridx as $key => $val)
			{
				$idx = intval($key);
				$tmprec = $bvattendance[$idx];
				array_push($bvattendancesrt, $tmprec);
			}
/*			
			for ($i=0; $i<count($bvattendancesrt); $i++)
			{
				$tmprec = $bvattendancesrt[$i];
				echo $tmprec[0].",".$tmprec[1].",".$tmprec[2].",".$tmprec[3].",".$tmprec[4]."</br>";
			}

			echo "bvattendancesrt size: ".count($bvattendancesrt)."</br>";
			die;
*/
			return $bvattendancesrt;			
		}
/*
		//Get Balavihar attendance for specified class and week
		public function getBalaviharAttendance( $curyear, $centername, $session, $grade, $seq )
		{
			$bvattendance = array();

			$tmp = $this->db->query('
								SELECT
								bvwx.bv_week_xref_id,
								p.first_name,
								p.last_name,
								bvwx.status
								FROM bv_week_xref bvwx
								INNER JOIN bv_reg_history brh ON brh.bv_reg_history_id=bvwx.bv_reg_history_id_fk
								INNER JOIN person p ON p.person_id=brh.person_id_fk
								INNER JOIN bv_week bvw ON bvw.bv_week_id=bvwx.bv_week_id_fk
								INNER JOIN cmb_center cc ON cc.cmb_center_id=brh.cmb_center_id_fk
								WHERE
								brh.year = ' . $this->db->escape($curyear) . '
								AND bvw.seq = ' . $this->db->escape($seq) . '
								AND bvw.year = ' . $this->db->escape($curyear) . '
								AND cc.center_name = ' . $this->db->escape($centername) . '
								AND brh.session = ' . $this->db->escape($session) . '
								AND brh.grade = ' . $this->db->escape($grade) . '
								ORDER BY p.last_name ASC
							');
							 
			$numres = $tmp->num_rows();
			
			if( $numres > 0)
			{
				foreach ($tmp->result() as $wkRow)
				{				
					$bvattendancerec = array();
				
					array_push($bvattendancerec, $wkRow->bv_week_xref_id);
					array_push($bvattendancerec, $wkRow->first_name);
					array_push($bvattendancerec, $wkRow->last_name);
					array_push($bvattendancerec, $wkRow->status);

					array_push($bvattendance, $bvattendancerec);
				}
			}
			
			return $bvattendance;			
		}
*/
		//Update attendance status for specified attendance record
		public function updateBalaviharAttendance( $bv_attend_xref_id, $status  )
		{
			$tmp = $this->db->query('
								UPDATE bv_attend_xref
									   SET status = ' . $this->db->escape($status) . '
								WHERE
									bv_attend_xref_id = ' . $this->db->escape($bv_attend_xref_id) . '
							');
							 
			return $bv_attend_xref_id;			
		}
/*
		//Update attendance status for specified attendance record
		public function updateBalaviharAttendance( $bv_week_xref_id, $status  )
		{
			$weekarr = array();
						
			$tmp = $this->db->query('
								UPDATE bv_week_xref
									   SET status = ' . $this->db->escape($status) . '
								WHERE
									bv_week_xref_id = ' . $this->db->escape($bv_week_xref_id) . '
							');
							 
			return $bv_week_xref_id;			
		}
*/
/*
		//Initialize attendance for all weeks for a given Balavihar entry
		public function createBalaviharAttendance( $bvRegHistoryId, $curyear )
		{
			$weekidarr = array();
						
			$tmp = $this->db->query('
								SELECT bw.bv_week_id
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
								ORDER BY bw.bv_week_id ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			if( $numweeks > 0)
			{
				$weekidarr = array();
				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekidarr, $wkRow->bv_week_id);
				}
				
				$count = 0;
				for( $i = 0; $i < $numweeks; $i++ )
				{

					//Check if this entry is already in table
					$tmp = $this->db->query('
								SELECT bvx.bv_week_xref_id
								FROM bv_week_xref bvx
								WHERE
									bvx.bv_week_id_fk = ' . $this->db->escape($weekidarr[$i]) . '
									AND bvx.bv_reg_history_id_fk = ' . $this->db->escape($bvRegHistoryId) . '
							');

							
					if ($tmp->num_rows() <= 0)
					{
						//Do insert if entry does not exist
						$tmp = $this->db->query('
							INSERT INTO bv_week_xref (bv_week_id_fk,bv_reg_history_id_fk,status)
							VALUES ('.$weekidarr[$i].','.$bvRegHistoryId.',2)
						');
						
						$count++;
					}
				}
				
				return $count;;
			}
			else
				return -1;
		}
*/
		//Initialize attendance for all weeks for a given Balavihar entry
		public function createBalaviharAttendance( $personId, $curyear )
		{
			$weekidarr = array();
						
			$tmp = $this->db->query('
								SELECT bw.bv_week_id
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
								ORDER BY bw.bv_week_id ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			if( $numweeks > 0)
			{
				$weekidarr = array();
				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekidarr, $wkRow->bv_week_id);
				}
				
				$count = 0;
				for( $i = 0; $i < $numweeks; $i++ )
				{

					//Check if this entry is already in table
					$tmp = $this->db->query('
								SELECT bvx.bv_attend_xref_id
								FROM bv_attend_xref bvx
								WHERE
									bvx.bv_week_id_fk = ' . $this->db->escape($weekidarr[$i]) . '
									AND bvx.person_id_fk = ' . $this->db->escape($personId) . '
									AND bvx.year = ' . $this->db->escape($curyear) . '
							');

							
					if ($tmp->num_rows() <= 0)
					{
						//Do insert if entry does not exist
						$tmp = $this->db->query('
							INSERT INTO bv_attend_xref (bv_week_id_fk,person_id_fk,year,status)
							VALUES ('.$weekidarr[$i].','.$personId.','.$curyear.',2)
						');
						
						$count++;
					}
				}
				
				return $count;
			}
			else
				return -1;
		}

		public function insertBVRegistration( $personId, $cmbCenterId, $curyear, $dates, $childInfoArray )
		{
			$source = "Online";
			
			$tgid = $this->getTeacherGroupID($cmbCenterId, $curyear, $childInfoArray[6], $childInfoArray[5]);
			
			$str_hash = md5($this->encrypt->get_random_password(32,32));

			if ($tgid > 0)
			{
				$this->db->query('
									INSERT INTO			bv_reg_history
														(
															person_id_fk,
															cmb_center_id_fk,
															year,
															grade,
															session,
															date_bv_begin,
															date_bv_end,
															date_reg_open,
															date_reg_close,
															date_registered,
															enabled,
															teacher_group_id_fk,
															str_hash
														)
									VALUES				(
															' . $this->db->escape($personId)  . ',
															' . $this->db->escape($cmbCenterId)  . ',
															' . $this->db->escape($curyear)  . ',
															' . $this->db->escape($childInfoArray[5])  . ',
															' . $this->db->escape($childInfoArray[6])  . ',
															' . $this->db->escape($dates[0])  . ',
															' . $this->db->escape($dates[1])  . ',
															' . $this->db->escape($dates[2])  . ',
															' . $this->db->escape($dates[3])  . ',
															Now(),
															1,
															' . $this->db->escape($tgid)  . ',
															' . $this->db->escape($str_hash) . '
														)
								 ');
			}
			else
			{
				$this->db->query('
									INSERT INTO			bv_reg_history
														(
															person_id_fk,
															cmb_center_id_fk,
															year,
															grade,
															session,
															date_bv_begin,
															date_bv_end,
															date_reg_open,
															date_reg_close,
															date_registered,
															enabled,
															teacher_group_id_fk,
															str_hash
														)
									VALUES				(
															' . $this->db->escape($personId)  . ',
															' . $this->db->escape($cmbCenterId)  . ',
															' . $this->db->escape($curyear)  . ',
															' . $this->db->escape($childInfoArray[5])  . ',
															' . $this->db->escape($childInfoArray[6])  . ',
															' . $this->db->escape($dates[0])  . ',
															' . $this->db->escape($dates[1])  . ',
															' . $this->db->escape($dates[2])  . ',
															' . $this->db->escape($dates[3])  . ',
															Now(),
															1,
															NULL,
															' . $this->db->escape($str_hash) . '
														)
								 ');
				
			}
			
			$bvRegHistoryId = $this->db->query('
													SELECT 		bv_reg_history_id 
													FROM 		bv_reg_history
													WHERE		str_hash = ' . $this->db->escape($str_hash) . '
												  ');

			if( $bvRegHistoryId->num_rows() == 1 )
			{
				$bvRegHistoryId = $bvRegHistoryId->row()->bv_reg_history_id;
				//echo $bvRegHistoryId."</br>";
				//die;
				
				//Create attendance entries for all weeks
				$this->createBalaviharAttendance($personId, $curyear);
			}
			else
				$bvRegHistoryId = 0;

			//$bvRegHistoryId = getMaxDbId( 'bv_reg_history_id', 'bv_reg_history' );
			
			return $bvRegHistoryId;
		}
		
		//Initialize attendance for all weeks for a given Balavihar entry
		public function createLanguageAttendance( $langRegHistoryId, $curyear )
		{
			$weekidarr = array();
						
			$tmp = $this->db->query('
								SELECT lw.lang_week_id
								FROM lang_week lw
								WHERE
									lw.year = ' . $this->db->escape($curyear) . '
								ORDER BY lw.lang_week_id ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			if( $numweeks > 0)
			{
				$weekidarr = array();
				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekidarr, $wkRow->lang_week_id);
				}
				
				$count = 0;
				for( $i = 0; $i < $numweeks; $i++ )
				{

					//Check if this entry is already in table
					$tmp = $this->db->query('
								SELECT lx.lang_week_xref_id
								FROM lang_week_xref lx
								WHERE
									lx.lang_week_id_fk = ' . $this->db->escape($weekidarr[$i]) . '
									AND lx.lang_reg_history_id_fk = ' . $this->db->escape($langRegHistoryId) . '
							');

							
					if ($tmp->num_rows() <= 0)
					{
						//Do insert if entry does not exist
						$tmp = $this->db->query('
							INSERT INTO lang_week_xref (lang_week_id_fk,lang_reg_history_id_fk,status)
							VALUES ('.$weekidarr[$i].','.$langRegHistoryId.',2)
						');
						
						$count++;
					}
				}
				
				return $count;;
			}
			else
				return -1;
		}

		public function insertLangRegistration( $personId, $cmbCenterId, $langId, $langlevel, $langsess, $curyear, $dates )
		{
			$source = "Online";
			$str_hash = md5($this->encrypt->get_random_password(32,32));
			
			$this->db->query('
								INSERT INTO			lang_reg_history
													(
														person_id_fk,
														cmb_center_id_fk,
														language_id_fk,
														year,
														session,
														date_begin,
														date_end,
														enabled,
														date_reg_open,
														date_reg_close,
														date_registered,
														level,
														str_hash
													)
								VALUES				(
														' . $this->db->escape($personId)  . ',
														' . $this->db->escape($cmbCenterId)  . ',
														' . $this->db->escape($langId)  . ',
														' . $this->db->escape($curyear)  . ',
														' . $this->db->escape($langsess)  . ',
														' . $this->db->escape($dates[0])  . ',
														' . $this->db->escape($dates[1])  . ',
														1,
														' . $this->db->escape($dates[2])  . ',
														' . $this->db->escape($dates[3])  . ',
														Now(),
														' . $this->db->escape($langlevel)  . ',
														' . $this->db->escape($str_hash) . '
													)
							 ');
			
			$langRegHistoryId = $this->db->query('
													SELECT 		lang_reg_history_id 
													FROM 		lang_reg_history
													WHERE		str_hash = ' . $this->db->escape($str_hash) . '
												  ');

			if( $langRegHistoryId->num_rows() == 1 )
			{
				$langRegHistoryId = $langRegHistoryId->row()->lang_reg_history_id;

				//Create attendance entries for all weeks
				$this->createLanguageAttendance($langRegHistoryId, $curyear);
			}
			else
				$langRegHistoryId = 0;

			//$langRegHistoryId = getMaxDbId( 'lang_reg_history_id', 'lang_reg_history' );
			
			return $langRegHistoryId;
		}

		public function getUnpaidCheckInfo( $curyear )
		{
			
			$unpaidchecklist = $this->db->query('
												SELECT    	crh.cmb_reg_history_id,
															p.first_name,
												          	p.last_name,
															p.gender,
															p.address1,
															p.address2,
															p.city,
															p.state,
															p.zip,
															p.email1,
															crh.amount_paid
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												INNER JOIN cmb_center cc ON cc.cmb_center_id=crh.cmb_center_id_fk
												WHERE       crh.fully_paid = 0
												AND 		p.enabled = 1
												AND 		p.cmb_member = 1
												AND			crh.payment_type = ' . $this->db->escape( 'check' ) . '
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
											  ');

			
			$regInfo = array();
			if( $unpaidchecklist->num_rows() > 0)
			{
				foreach ($unpaidchecklist->result() as $regIdRow)
				{
					$personInfo = array();		
					array_push($personInfo, $regIdRow->cmb_reg_history_id);					
					array_push($personInfo, $regIdRow->first_name);					
					array_push($personInfo, $regIdRow->last_name);					
					array_push($personInfo, $regIdRow->address1);					
					array_push($personInfo, $regIdRow->city);					
					array_push($personInfo, $regIdRow->state);					
					array_push($personInfo, $regIdRow->zip);					
					array_push($personInfo, $regIdRow->email1);
					array_push($personInfo, $regIdRow->amount_paid);
					
					array_push($regInfo, $personInfo);					
				}
			}
				
			return $regInfo;
		}

		public function getUnRegisteredMemberInfo( $curyear )
		{
			$prevyear = $curyear - 1;
			
			$urmemlist = $this->db->query('
						SELECT 	IFNULL(crh.home_url,' . $this->db->escape( '' ) . ') AS home_url,
								p.person_name,
								p.password_nh,
								p.first_name,
								IFNULL(p.last_name,' . $this->db->escape( '' ) . ') AS last_name,
								p.gender,
								p.address1,
								p.address2,
								p.city,
								p.state,
								p.zip,
								p.email1,
								crh.cmb_reg_history_id
						FROM person p
						INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
						INNER JOIN household hh ON hh.household_id=hxr.household_id_fk
						INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hh.household_id
						WHERE
						p.cmb_member=1
						AND p.enabled = 1
						AND crh.year = ' . $this->db->escape( $prevyear ) . '
						AND p.first_name <> ' . $this->db->escape( 'Admin' ) . '
						AND hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
						AND crh.household_id_fk NOT IN (
						SELECT
						crh.household_id_fk
						FROM person p
						INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
						INNER JOIN household hh ON hh.household_id=hxr.household_id_fk
						INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hh.household_id
						WHERE
						p.cmb_member=1
						AND p.enabled = 1
						AND crh.year = ' . $this->db->escape( $curyear ) . '
						AND p.first_name <> ' . $this->db->escape( 'Admin' ) . '
						AND hxr.member_type = ' . $this->db->escape( 'Primary' ) . ')
						ORDER BY p.last_name ASC;
					');
								
			$unregInfo = array();
			if( $urmemlist->num_rows() > 0)
			{
				foreach ($urmemlist->result() as $unregRow)
				{
					$cmbRegHistoryId = $unregRow->cmb_reg_history_id;
					$memURLKey = $unregRow->home_url;
					if ($memURLKey == "")
					{
						//Create home url if it doesn't already exist
						$memURLKey = $this->createRegistrationURLKey( $cmbRegHistoryId );
					}

					if ($memURLKey != "")
					{
						$personInfo = array();		
						array_push($personInfo, $memURLKey);
						array_push($personInfo, $unregRow->person_name);
						array_push($personInfo, $unregRow->password_nh);					
						array_push($personInfo, $unregRow->first_name);					
						array_push($personInfo, $unregRow->last_name);					
						array_push($personInfo, $unregRow->address1);					
						array_push($personInfo, $unregRow->city);					
						array_push($personInfo, $unregRow->state);					
						array_push($personInfo, $unregRow->zip);					
						array_push($personInfo, $unregRow->email1);
										
						array_push($unregInfo, $personInfo);
					}
				}
			}
				
			return $unregInfo;
		}

		public function getAllRegisteredMemberInfo()
		{
			$urmemlist = $this->db->query('
						SELECT 	IFNULL(crh.home_url,' . $this->db->escape( '' ) . ') AS home_url,
								p.person_name,
								p.password_nh,
								p.first_name,
								IFNULL(p.last_name,' . $this->db->escape( '' ) . ') AS last_name,
								p.gender,
								p.address1,
								p.address2,
								p.city,
								p.state,
								p.zip,
								p.email1,
								crh.cmb_reg_history_id
						FROM person p
						INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
						INNER JOIN household hh ON hh.household_id=hxr.household_id_fk
						INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hh.household_id
						WHERE
						p.cmb_member = 1
						AND p.enabled = 1
						AND crh.enabled = 1
						AND p.first_name <> ' . $this->db->escape( 'Admin' ) . '
						AND hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
						GROUP BY p.person_id
						ORDER BY p.last_name ASC;
					');
								
			$regInfo = array();
			if( $urmemlist->num_rows() > 0)
			{
				foreach ($urmemlist->result() as $regRow)
				{
					$cmbRegHistoryId = $regRow->cmb_reg_history_id;
					$memURLKey = $regRow->home_url;
					if ($memURLKey == "")
					{
						//Create home url if it doesn't already exist
						$memURLKey = $this->createRegistrationURLKey( $cmbRegHistoryId );
					}

					if ($memURLKey != "")
					{
						$personInfo = array();		
						array_push($personInfo, $memURLKey);
						array_push($personInfo, $regRow->person_name);
						array_push($personInfo, $regRow->password_nh);					
						array_push($personInfo, $regRow->first_name);					
						array_push($personInfo, $regRow->last_name);					
						array_push($personInfo, $regRow->address1);					
						array_push($personInfo, $regRow->city);					
						array_push($personInfo, $regRow->state);					
						array_push($personInfo, $regRow->zip);					
						array_push($personInfo, $regRow->email1);
										
						array_push($regInfo, $personInfo);
					}
				}
			}
				
			return $regInfo;
		}

		public function getUnpaidCheckRegId( $curyear )
		{
			
			$unpaidchecklist = $this->db->query('
												SELECT    	crh.cmb_reg_history_id
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												INNER JOIN cmb_center cc ON cc.cmb_center_id=crh.cmb_center_id_fk
												WHERE       crh.fully_paid = 0
												AND 		p.enabled = 1
												AND 		p.cmb_member = 1
												AND			crh.payment_type = ' . $this->db->escape( 'check' ) . '
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
											  ');

			
			$regId = array();
			if( $unpaidchecklist->num_rows() > 0)
			{
				foreach ($unpaidchecklist->result() as $regIdRow)
				{
					array_push($regId, $regIdRow->cmb_reg_history_id);					
				}
			}
				
			return $regId;
		}

		public function getURLKeyFromPersonId( $personId, $curyear )
		{
			
			$tmp = $this->db->query('
												SELECT    	crh.home_url
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												INNER JOIN cmb_center cc ON cc.cmb_center_id=crh.cmb_center_id_fk
												WHERE       p.person_id = ' . $this->db->escape( $personId ) . '		
												AND 		p.enabled = 1
												AND 		p.cmb_member = 1
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
												LIMIT 1;
											  ');

			
			$urlKey = "";
			if( $tmp->num_rows() > 0)
			{
				$urlKey = $tmp->row()->home_url;
			}
				
			return $urlKey;
		}

		public function updateCMBRegHistoryFees($cmbRegHistoryId, $regFee, $bvFee, $langFee)
		{
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				registration_amount = ' . $this->db->escape($regFee) . ',
												bv_amount = ' . $this->db->escape($bvFee) . ',
												lang_amount = ' . $this->db->escape($langFee) . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;			
		}

		public function updateCMBRegHistoryComments( $cmbRegHistoryId, $feedback )
		{
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				comments = ' . $this->db->escape($feedback) . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;			
		}

		public function confirmCMBRegistration( $cmbRegHistoryId, $tot_paid, $txn_id )
		{
			
			//Hack to set [sent_reg_confirm_email = 0], because email from paypal ipn doesn't work
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				fully_paid = 1,
												payment_status = 1,
												amount_paid = ' . $this->db->escape($tot_paid) . ',
												payment_transaction_id = ' . $this->db->escape($txn_id) . ',
												payment_type = ' . $this->db->escape('paypal') . ',
												source = ' . $this->db->escape('Online') . ',
												sent_reg_confirm_email = 0,
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		public function changeCMBRegistrationPaypal( $cmbRegHistoryId )
		{
			
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				payment_type = ' . $this->db->escape('paypal') . ',
												source = ' . $this->db->escape('Online') . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		public function confirmCMBRegistrationNoPay( $cmbRegHistoryId )
		{
			
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				fully_paid = 0,
												amount_paid = 0,
												fully_paid = 1,
												payment_status = 1,
												payment_type = ' . $this->db->escape('na') . ',
												source = ' . $this->db->escape('na') . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		public function confirmCMBRegistrationCheck( $cmbRegHistoryId, $tot_paid )
		{
			
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				fully_paid = 0,
												payment_status = 2,
												amount_paid = ' . $this->db->escape($tot_paid) . ',
												payment_type = ' . $this->db->escape('check') . ',
												source = ' . $this->db->escape('Offline') . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		public function setCMBRegistrationPending( $cmbRegHistoryId )
		{
			
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				payment_status = 2,
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		public function completeCMBRegistrationCheck( $cmbRegHistoryId, $checknum )
		{
			
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				fully_paid = 1,
												payment_status = 1,
												check_num = ' . $this->db->escape($checknum) . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
								AND				payment_type = ' . $this->db->escape('check') . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		public function getCMBRegistrationPaymentStatus( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			crh.payment_status
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->payment_status;
			else
				return -1;
		}

		public function getCMBRegistrationPaymentType( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			crh.payment_type
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->payment_type;
			else
				return "";
		}

		public function getCMBRegistrationMemberType( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			crh.type
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->type;
			else
				return "";
		}

		public function getCMBRegistrationPaymentCheckNum( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			IFNULL(crh.check_num,' . $this->db->escape('') . ') AS check_num
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->check_num;
			else
				return "";
		}

		public function getCMBRegistrationPaymentPaypalID( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			IFNULL(crh.payment_transaction_id,' . $this->db->escape('') . ') AS payment_transaction_id
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->payment_transaction_id;
			else
				return "";
		}

		public function getCMBRegistration( $hhId, $curyear, $cmbCenterId )
		{
			
			$tmp = $this->db->query('
								SELECT			crh.cmb_reg_history_id
								FROM			cmb_reg_history crh
								WHERE			crh.household_id_fk = ' . $this->db->escape($hhId) . ' AND
												crh.year = ' . $this->db->escape($curyear) . ' AND
												crh.cmb_center_id_fk = ' . $this->db->escape($cmbCenterId) . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return $tmp->row()->cmb_reg_history_id;
			else
				return -1;
		}

		//Delete an existing CMB registration
		public function deleteCMBRegistration( $cmbRegHistoryId )
		{
			
			$this->db->query('
								DELETE
								FROM			cmb_reg_history
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;
		}

		//Delete object from 'paypal_temp'
		public function deleteRegTempObj( $paypalTempId )
		{
			
			$this->db->query('
								DELETE
								FROM			paypal_temp
								WHERE			paypal_temp_id = ' . $paypalTempId  . '
							 ');
							 
			return $paypalTempId;
		}

		//Delete existing Balavihar registration
		public function deleteBVRegistration( $childPersonIdArray, $curyear )
		{
			
			$numchildren = count($childPersonIdArray);
			if ($numchildren > 0)
			{
				$pidstr = $childPersonIdArray[0];
				
				for ($i = 1; $i < $numchildren; $i++)
				{
					$pidstr = $pidstr.",".$childPersonIdArray[$i];
				}
			
				$this->db->query('
									DELETE
									FROM			bv_reg_history
									WHERE			person_id_fk IN (' . $pidstr  . ')
									AND				year = ' . $this->db->escape($curyear) . '
							 	');				
			}
							 
			return 0;
		}

		//Delete existing Language registration
		public function deleteLangRegistration( $childPersonIdArray, $childLangArray, $curyear )
		{
			
			$numchildren = count($childPersonIdArray);
			if ($numchildren > 0)
			{
				for ($i = 0; $i < $numchildren; $i++)
				{
					if ($childLangArray[$i] != "None")
					{			
						$this->db->query('
										DELETE
										FROM			lang_reg_history
										WHERE			person_id_fk = ' . $this->db->escape($childPersonIdArray[$i]) . '
										AND				year = ' . $this->db->escape($curyear) . '
							 		');
					}
				}
			}
							 
			return 0;
		}

		public function isRegistrationPaid( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			crh.fully_paid
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $this->db->escape($cmbRegHistoryId) . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				if ($tmp->row()->fully_paid == 1)
					return TRUE;
				else
					return FALSE;
			else
				return FALSE;
		}

		public function isClassFull( $year, $center, $grade, $session )
		{
			
			$tmp = $this->db->query('
								SELECT
									brh.grade AS ' . $this->db->escape("grade") . ',
									COUNT(brh.grade) AS ' . $this->db->escape("numchildren") . '
								FROM bv_reg_history brh
									INNER JOIN cmb_center cc ON cc.cmb_center_id=brh.cmb_center_id_fk
								WHERE
									brh.enabled = 1
									AND brh.year = ' . $this->db->escape($year) . '
									AND cc.center_name = ' . $this->db->escape($center) . '
									AND brh.grade = ' . $this->db->escape($grade) . '
									AND brh.session = ' . $this->db->escape($session) . '
							');
							 
			if( $tmp->num_rows() > 0)
			{
				if ($center == "Andover")
                {
    				if ($tmp->row()->numchildren < $this->config->item('max_class_size'))
    				    return FALSE;
    				else
    				    return TRUE;
                }
                else
                {
                    if ($tmp->row()->numchildren < $this->config->item('max_class_size_mw'))
                        return FALSE;
                    else
                        return TRUE;                   
                }
			}
			else
				return FALSE;
		}

        public function isValidDOB( $dob )
        {
            $dob_thr_dt = strtotime($this->config->item('dob_thr_date'));
            $dob_dt = strtotime($dob);
            
            if ($dob_dt > $dob_thr_dt)
                return FALSE;
            else
                return TRUE;
        }

		public function isRegisteredByCheck( $cmbRegHistoryId )
		{
			
			$tmp = $this->db->query('
								SELECT			crh.payment_type
								FROM			cmb_reg_history crh
								WHERE			crh.cmb_reg_history_id = ' . $this->db->escape($cmbRegHistoryId) . '
								AND				crh.payment_type = ' . $this->db->escape("check") . '
							 ');
							 
			if( $tmp->num_rows() > 0)
				return TRUE;
			else
				return FALSE;
		}

		//Determines if child is registered in Balavihar for current year
		public function isChildRegisteredBV( $personId, $current_year, $cmbcenterId )
		{
			$checkRegistration = $this->db->query('
													SELECT		brh.bv_reg_history_id
													FROM		bv_reg_history brh
													WHERE 		brh.person_id_fk = ' . $this->db->escape($personId) . '
													AND			brh.cmb_center_id_fk = ' . $this->db->escape($cmbcenterId) . '
													AND 		brh.year = ' . $this->db->escape($current_year) . '
											    ');
			
			$childIsRegistered = FALSE;
			if( $checkRegistration->num_rows() > 0 )
			{
				$childIsRegistered = TRUE;
			}
			
			return $childIsRegistered;
		}

		//Determines if child is registered in language class for current year
		public function isChildRegisteredLang( $personId, $current_year, $cmbcenterId, $langId )
		{
			$checkRegistration = $this->db->query('
													SELECT		lrh.lang_reg_history_id
													FROM		lang_reg_history lrh
													WHERE 		lrh.person_id_fk = ' . $this->db->escape($personId) . '
													AND			lrh.cmb_center_id_fk = ' . $this->db->escape($cmbcenterId) . '
													AND			lrh.language_id_fk = ' . $this->db->escape($langId) . '
													AND 		lrh.year = ' . $this->db->escape($current_year) . '
											    ');
			
			$childIsRegistered = FALSE;
			if( $checkRegistration->num_rows() > 0 )
			{
				$childIsRegistered = TRUE;
			}
			
			return $childIsRegistered;
		}

        //Check if registration is open
        public function isRegistrationOpen()
        {
            $this->load->helper('string');       
            $curdate = date('Y-m-d H:i:s', time());
            
            //Find password key create date
            $tmp = $this->db->query('           
                                                SELECT      date_reg_open,
                                                            date_reg_close     
                                                FROM        bv_event
                                                WHERE       STR_TO_DATE(' . $this->db->escape($curdate)  . ',' . $this->db->escape("%Y-%m-%d %T")  . ') BETWEEN date_reg_open AND date_reg_close
                                                AND         year = ' . $this->db->escape($this->getCurrentYear()) . '
                                            ');
            
            if( $tmp->num_rows() > 0)
            {
                return TRUE;           
            }
            else
                return FALSE;
        }

        //Check if registration is open
        public function isTeacherRegistrationOpen()
        {
            $this->load->helper('string');       
            $curdate = date('Y-m-d H:i:s', time());
            
            //Find password key create date
            $tmp = $this->db->query('           
                                                SELECT      date_reg_open,
                                                            date_reg_close     
                                                FROM        bv_event
                                                WHERE       STR_TO_DATE(' . $this->db->escape($curdate)  . ',' . $this->db->escape("%Y-%m-%d %T")  . ') BETWEEN date_treg_open AND date_reg_close
                                                AND         year = ' . $this->db->escape($this->getCurrentYear()) . '
                                            ');
            
            if( $tmp->num_rows() > 0)
            {
                return TRUE;           
            }
            else
                return FALSE;
        }

		public function getCurrentBVEventDates( $currentyear )
		{
			$bvdates = $this->db->query('
													SELECT		be.date_bv_begin as date_bv_begin,
													be.date_bv_end as date_bv_end,
													be.date_reg_open as date_reg_open,
													be.date_reg_close as date_reg_close,
													be.date_treg_open as date_treg_open
													FROM		bv_event be
													WHERE		be.year = ' . $this->db->escape($currentyear) . '
											    ');
												
			$dates = array();

			if( $bvdates->num_rows() > 0 )
			{
				array_push($dates, $bvdates->first_row()->date_bv_begin);
				array_push($dates, $bvdates->first_row()->date_bv_end);			
				array_push($dates, $bvdates->first_row()->date_reg_open);
				array_push($dates, $bvdates->first_row()->date_reg_close);
                array_push($dates, $bvdates->first_row()->date_treg_open);
			}
			
			//echo count($dates)."</br>";
			//echo $dates[0].",".$dates[1].",".$dates[2].",".$dates[3]."</br>";
			//die;
			
			return $dates;
		}
		
		public function getCurrentLangEventDates()
		{
			$langdates = $this->db->query('
													SELECT		le.date_begin as date_begin,
													le.date_end as date_end,
													le.date_reg_open as date_reg_open,
													le.date_reg_close as date_reg_close
													FROM		language_event le
											    ');
												
			$dates = array();
			if( $langdates->num_rows() > 0 )
			{
				array_push($dates, $langdates->first_row()->date_begin);
				array_push($dates, $langdates->first_row()->date_end);			
				array_push($dates, $langdates->first_row()->date_reg_open);
				array_push($dates, $langdates->first_row()->date_reg_close);
			}
			
			return $dates;
		}

		public function isHouseholdRegistered( $hhId, $current_year, $cmbCenterId )
		{
			$checkRegistration = $this->db->query('
													SELECT		crh.cmb_reg_history_id
													FROM		cmb_reg_history crh
													WHERE 		crh.household_id_fk = ' . $this->db->escape($hhId) . ' 
													AND 		crh.year = ' . $this->db->escape($current_year) . '
													AND			crh.cmb_center_id_fk = ' . $this->db->escape($cmbCenterId) . '
											    ');
			
			$hhIsRegistered = FALSE;
			if( $checkRegistration->num_rows() > 0 )
			{
				$hhIsRegistered = TRUE;
			}
			
			return $hhIsRegistered;
		}
				
		public function getCurrentYear()
		{
			$currentyear = $this->db->query('
													SELECT		CAST(ce.current_year AS CHAR(4)) AS current_year
													FROM		cmb_event ce
											    ');
												
			return $currentyear->first_row()->current_year;
		}
		
		public function getCurrentRegAmount($cmbcentername)
		{
			$regamt = $this->db->query('
													SELECT		ce.registration_fee AS reg_fee
													FROM		cmb_event ce
													INNER JOIN  cmb_center cc ON cc.cmb_center_id = ce.cmb_center_id_fk
													WHERE cc.center_name = ' . $this->db->escape($cmbcentername) . '
											    ');
												
			if( $regamt->num_rows() > 0 )
				return $regamt->first_row()->reg_fee;
			else
				return 0.0;
		}

		public function getLangId($langName)
		{
			$langId = $this->db->query('
													SELECT		l.language_id as language_id
													FROM		language l
													WHERE		l.language_name = ' . $this->db->escape($langName) . '
											    ');
												
			return $langId->first_row()->language_id;
		}

		public function getCMBCenterId($cmbCenter)
		{
			$cmbCenterId = $this->db->query('
													SELECT		cc.cmb_center_id as cmb_center_id
													FROM		cmb_center cc
													WHERE		cc.center_name = ' . $this->db->escape($cmbCenter) . '
											    ');
												
			return $cmbCenterId->first_row()->cmb_center_id;
		}
		
		
		public function getMemberURLKey( $CMBRegHistoryId )
		{
			$getHomeUrl = $this->db->query('
												SELECT 	home_url 
												FROM 	cmb_reg_history c
												WHERE 	cmb_reg_history_id = ' . $this->db->escape( $CMBRegHistoryId ) . '
											');	
												
			if ($getHomeUrl->num_rows() > 0)
				return $getHomeUrl->first_row()->home_url;
			else
				return "";
		}

		//Get all teacher groups associated with person
		public function getTeacherGroups( $personId )
		{
			$teachergroups = array();
			
			$tmp = $this->db->query('
												SELECT 	tgx.teacher_group_id_fk 
												FROM 	teacher_group_xref tgx
												WHERE 	tgx.person_id_fk = ' . $this->db->escape( $personId ) . '
												AND		tgx.enabled = 1
												ORDER BY tgx.teacher_group_id_fk ASC
											');	
												
			if ($tmp->num_rows() > 0)
			{
				foreach ($tmp->result() as $grpRow)
				{
					array_push($teachergroups, $grpRow->teacher_group_id_fk);
				}
			}
				
			return $teachergroups;
		}

		//Get grade for specified teacher group
		public function getGradeFromTeacherGroups( $tgid )
		{
			$tmp = $this->db->query('
												SELECT 	tg.grade 
												FROM 	teacher_group tg
												WHERE 	tg.teacher_group_id = ' . $this->db->escape( $tgid ) . '
												AND		tg.enabled = 1
											');	
												
			if ($tmp->num_rows() > 0)
			{
				return $tmp->row()->grade;
			}
			else
				return "";
		}

		//Get session for specified teacher group
		public function getSessionFromTeacherGroups( $tgid )
		{
			$tmp = $this->db->query('
												SELECT 	tg.session 
												FROM 	teacher_group tg
												WHERE 	tg.teacher_group_id = ' . $this->db->escape( $tgid ) . '
												AND		tg.enabled = 1
											');	
												
			if ($tmp->num_rows() > 0)
			{
				return $tmp->row()->session;
			}
			else
				return "";
		}

		//Get center for specified teacher group
		public function getCenterFromTeacherGroups( $tgid )
		{
			$tmp = $this->db->query('
												SELECT 	cc.center_name 
												FROM 	teacher_group tg
												INNER JOIN cmb_center cc ON cc.cmb_center_id=tg.cmb_center_id_fk
												WHERE 	tg.teacher_group_id = ' . $this->db->escape( $tgid ) . '
												AND		tg.enabled = 1
											');	
												
			if ($tmp->num_rows() > 0)
			{
				return $tmp->row()->center_name;
			}
			else
				return "";
		}

		//Get criteria associated with a teacher group
		public function getTeacherGroupCriteria( $curyear, $tg_id )
		{
			$tgcriteria = array();
			
			$tmp = $this->db->query('
												SELECT 	cc.center_name,
														tg.session,
														tg.grade 
												FROM 	teacher_group tg
												INNER JOIN cmb_center cc ON cc.cmb_center_id=tg.cmb_center_id_fk
												WHERE 	tg.year = ' . $this->db->escape( $curyear ) . '
												AND		tg.teacher_group_id = ' . $this->db->escape( $tg_id ) . '
												AND		tg.enabled = 1
											');	
												
			if ($tmp->num_rows() > 0)
			{
				foreach ($tmp->result() as $grpRow)
				{
					array_push($tgcriteria, $grpRow->center_name);
					array_push($tgcriteria, $grpRow->session);
					array_push($tgcriteria, $grpRow->grade);
				}
			}
				
			return $tgcriteria;
		}

		public function getCMBRegHistoryIdfromPerson( $personId )
		{
			$tmp = $this->db->query('
										SELECT		crh.cmb_reg_history_id
										FROM		cmb_reg_history crh
										INNER JOIN	household_xref hxr ON hxr.person_id_fk = ' . $this->db->escape( $personId ) . '
										INNER JOIN	household hh ON hh.household_id = hxr.household_id_fk AND hh.household_id = crh.household_id_fk
									');
			
			if($tmp->num_rows() > 0)
			{
				return $tmp->first_row()->cmb_reg_history_id;
			}
			else
			{
				return 0;
			}
		}
		
		public function getCMBRegHistoryIdfromPersonYear( $personId, $curyear )
		{
			$tmp = $this->db->query('
										SELECT		crh.cmb_reg_history_id
										FROM		cmb_reg_history crh
										INNER JOIN	household_xref hxr ON hxr.person_id_fk = ' . $this->db->escape( $personId ) . '
										INNER JOIN	household hh ON hh.household_id = hxr.household_id_fk AND hh.household_id = crh.household_id_fk
										WHERE crh.year = ' . $this->db->escape( $curyear ) . '
									');
			
			if($tmp->num_rows() > 0)
			{
				return $tmp->first_row()->cmb_reg_history_id;
			}
			else
			{
				return 0;
			}
		}

		public function createRegistrationURLKey( $cmbRegHistoryId )
		{
			$urlKey = uniqid();
			
			//Check if this id already exists
			$tmpId = $this->getCMBRegHistoryIdfromURL( $urlKey );
			if ($tmpId > 0)
			{
				$urlKey = uniqid();
				$tmpId1 = $this->getCMBRegHistoryIdfromURL( $urlKey );
				if ($tmpId1 > 0)
				{
					return "";
				}
			}
			
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				home_url = ' . $this->db->escape($urlKey) . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $urlKey;			
		}
		
		public function getCMBTotalRegistrationFeefromURL( $urlKey )
		{
			$getRegistrant = $this->db->query('
												SELECT    	(crh.registration_amount + crh.donation_amount) as total_fee
												FROM 		cmb_reg_history crh
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												LIMIT		1;
											  ');

			if($getRegistrant->num_rows() > 0)
			{								
				return $getRegistrant->first_row()->total_fee;			  
			}
			else
				return 0.0;
		}
		
		public function getCMBRegHistoryIdfromURL( $urlKey )
		{
			$tmp = $this->db->query('
												SELECT    	crh.cmb_reg_history_id
												FROM 		cmb_reg_history crh
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												LIMIT		1;
											  ');

			if($tmp->num_rows() > 0)
			{								
				return $tmp->first_row()->cmb_reg_history_id;			  
			}
			else
				return 0;
		}

		public function getCMBRegHistoryIdfromURLYear( $urlKey, $curyear )
		{
			$tmp = $this->db->query('
												SELECT    	crh.cmb_reg_history_id
												FROM 		cmb_reg_history crh
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												LIMIT		1;
											  ');

			if($tmp->num_rows() > 0)
			{								
				return $tmp->first_row()->cmb_reg_history_id;			  
			}
			else
				return 0;
		}
		
		public function getTempIDCMBRegistration( $cmbRegHistoryId )
		{
			$tmp = $this->db->query('
												SELECT    	crh.obj_temp_id
												FROM 		cmb_reg_history crh
												WHERE       crh.cmb_reg_history_id = ' . $this->db->escape( $cmbRegHistoryId ) . '
												LIMIT		1;
											  ');

			if($tmp->num_rows() > 0)
			{								
				return $tmp->first_row()->obj_temp_id;			  
			}
			else
				return 0;
		}

		public function setCMBRegistrationTempId( $cmbRegHistoryId, $temp_id )
		{
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				obj_temp_id = ' . $this->db->escape($temp_id) . ',
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;						
		}

		public function unSetCMBRegistrationTempId( $cmbRegHistoryId )
		{
			$this->db->query('
								UPDATE			cmb_reg_history
								SET				obj_temp_id = 0,
												date_modified = Now()
								WHERE			cmb_reg_history_id = ' . $cmbRegHistoryId  . '
							 ');
							 
			return $cmbRegHistoryId;						
		}

		//Change of password email
		public function sendMemberChangePasswordEmail( $cwr_registrant_home_value, $emailto, $password )
		{			
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($cwr_registrant_home_value);
			
			$message = '';
			$message .= '<p>Hari OM ' . $cwr_registrant_home_value->getFirstName() . 'ji,</p>';
			$message .= '<p>Your password has changed.</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="150">New Password:</td><td>' . $password . '</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Your personal home page is: ' . base_url() . 'cmb/member/login_home/</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			/* $config['protocol'] = 'smtp';
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = 'html'; */
	
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');

			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			//$this->email->to( $cwr_registrant_home_value->getEmail1() );
			$this->email->to( $emailto );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('CMB Change of Password');
			$this->email->message($message);
			
			$this->email->send();
		}

		//Confirms registration for Life member with no payment
		public function sendMemberRegistrationConfirmEmailNoPay( $walkRegistration )
		{
			$this->load->library('encrypt');

			$walkRegistration = Member_registration_value::cast($walkRegistration);
			
			$curyear = $walkRegistration->getCurrentYear();
			$urlKey = $this->getMemberURLKey( $walkRegistration->getCMBRegHistoryId() );
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $urlKey, $curyear ));
			$numChildren = count($cwr_registrant_home_value->getChildInfo());
			
			$message = '';
			//$message .= '<p><b>You have Been Registered for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1).'.</b></p><p><b>You do not have to stop at the registration table on opening day.</b></p>';
			$message .= '<p><b>You have Been Registered for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1).'.</b></p>';
			$message .= '<p>Hari OM ' . $walkRegistration->getPerson()->getFirstName() . 'ji,</p>';
			$message .= '<p>Your registration for Chinmaya Mission Boston has been confirmed:</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="150">Username:</td><td>' . $walkRegistration->getPerson()->getUserName() . '</td></tr>';
			//$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($walkRegistration->getPerson()->getPassword()) . '</td></tr>';
			$message .= '<tr><td width="250">Secret question:</td><td>' . $walkRegistration->getPerson()->getSecretQuestion() . '</td></tr>';
			$message .= '<tr><td width="250">Secret answer:</td><td>' . $walkRegistration->getPerson()->getSecretPhrase() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Phone#:</td><td>' . $cwr_registrant_home_value->getPhone1() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Cell#:</td><td>' . $cwr_registrant_home_value->getPhone2() . '</td></tr>';
			if($cwr_registrant_home_value->getSpouseFirstName() != "")
			{
				$message .= '<tr><td width="250">Spouse Name:</td><td>' . $cwr_registrant_home_value->getSpouseFirstName() . ' ' . $cwr_registrant_home_value->getSpouseLastName() . '</td></tr>';				
				$message .= '<tr><td width="250">Spouse Member Email:</td><td>' . $cwr_registrant_home_value->getSpouseEmail1() . '</td></tr>';
				$message .= '<tr><td width="250">Spouse Member Phone#:</td><td>' . $cwr_registrant_home_value->getSpousePhone1() . '</td></tr>';
				$message .= '<tr><td width="250">Spouse Member Cell#:</td><td>' . $cwr_registrant_home_value->getSpousePhone2() . '</td></tr>';
			}
			$message .= '<tr><td width="250">Address:</td><td>' . $walkRegistration->getPerson()->getAddress1() . '</td></tr>';
			$message .= '<tr><td width="250">City:</td><td>' . $walkRegistration->getPerson()->getCity() . '</td></tr>';
			$message .= '<tr><td width="250">State:</td><td>' . $walkRegistration->getPerson()->getState() . '</td></tr>';
			$message .= '<tr><td width="250">Zip Code:</td><td>' . $walkRegistration->getPerson()->getZip() . '</td></tr>';
			if ($cwr_registrant_home_value->getOrgName() != "")
				$message .= '<tr><td width="250">Company name:</td><td>' . $cwr_registrant_home_value->getOrgName() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Center:</td><td>' . $cwr_registrant_home_value->getCMBCenter() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $walkRegistration->getCMBRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $walkRegistration->getBVRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Donation:</td><td>$' . $walkRegistration->getDonation() . '</td></tr>';
			$message .= '<tr><td width="250">Total:</td><td>$' . $walkRegistration->getTotalRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">No Payment for Life Member' . '</td></tr>';

			//Child Balavihar information
			if($numChildren > 0)
			{
				$numEnabledChildren = 0;
				$childInfoArray = $cwr_registrant_home_value->getChildInfo();
				for( $i = 0; $i < $numChildren; $i++ )
				{
					$childInfoRes = $childInfoArray[$i];
					if ($childInfoRes[7] == "1")
						$numEnabledChildren ++;
				}
		
				if ($numEnabledChildren > 0)
				{
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"><hr /></td></tr>';
					$message .= '<tr><td width="250"><b><u>Children Balavihar Info:<u><b></td></tr>';
				
					for( $i = 0; $i < $numChildren; $i++ )
					{
						$childInfoRes = $childInfoArray[$i];
						if ($childInfoRes[7] == "1")
						{
							$message .= '<tr><td width="250">Child '.($i+1).': '.$childInfoRes[0] . ' ' . $childInfoRes[1].'</td></tr>';
							$message .= '<tr><td width="250">Gender: '.$childInfoRes[2].'</td></tr>';
							$message .= '<tr><td width="250">DOB: '.substr($childInfoRes[4],0,10).'</td></tr>';
							if ($childInfoRes[3] != "")
								$message .= '<tr><td width="250">Email: '.$childInfoRes[3].'</td></tr>';
							$message .= '<tr><td width="250">Grade: '.$childInfoRes[5].'</td></tr>';
							$message .= '<tr><td width="250">Session: '.$childInfoRes[6].'</td></tr>';
						}
					
						$message .= '<tr><td width="250"><hr /></td></tr>';
					} //for( $i = 0; $i < $numChildren; $i++ )
				} //if ($numEnabledChildren > 0)
			} //if($numChildren > 0)

			//Child Language Info
			if($numChildren > 0)
			{
				$numEnabledChildren = 0;
				$childLangArray = $cwr_registrant_home_value->getChildLang();
				for( $i = 0; $i < $numChildren; $i++ )
				{
					$childInfoRes = $childInfoArray[$i];
					if (($childLangArray[$i] != "None"))
						$numEnabledChildren ++;
				}
		
				if ($numEnabledChildren > 0)
				{
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"><b><u>Children Language Info:<u><b></td></tr>';
				
					$childLangLevelArray = $cwr_registrant_home_value->getChildLangLevel();
					for( $i = 0; $i < $numChildren; $i++ )
					{
						$childInfoRes = $childInfoArray[$i];
						if (($childLangArray[$i] != "None"))
						{
							$message .= '<tr><td width="250">Child '.($i+1).': '.$childInfoRes[0] . ' ' . $childInfoRes[1].'</td></tr>';
							$message .= '<tr><td width="250">Language: '.$childLangArray[$i].'</td></tr>';
							$message .= '<tr><td width="250">Language Level: '.$childLangLevelArray[$i].'</td></tr>';
						}			
						
						$message .= '<tr><td width="250"><hr /></td></tr>';
					} //for( $i = 0; $i < $numChildren; $i++ )
				}
			} //if($numChildren > 0)

			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Login to your home page at : ' . base_url() . '</p>';
			$message .= '<p>You will be contacted with the details of your registration. We would like to thank you for your support.</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( $walkRegistration->getPerson()->getEmail1() );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('Registration confirmation for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
		}
	
		public function log_cmb_registration($message) 
		{
			if (!$this->config->item('cmb_log')) return;  // is logging turned off?

			// Write to log
			$fp=fopen($this->config->item('cmb_log_file'),'a');
			fwrite($fp, '['.date('m/d/Y g:i A').'] - ' . $message . "\n\n"); 

			fclose($fp);  // close file
		}

		//Confirms registration after payment is confirmed through Paypal ipn
		public function sendMemberRegistrationConfirmEmailPaypal( $walkRegistration, $paypal_txn_id )
		{
			$this->load->library('encrypt');
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('proxies/Cwr_registrant_member_proxy');

			$walkRegistration = Member_registration_value::cast($walkRegistration);
			
			$curyear = $this->getCurrentYear();
			$urlKey = $this->getMemberURLKey( $walkRegistration->getCMBRegHistoryId() );

			$this->log_cmb_registration("Entered Confirm Email from Paypal ipn");
			$this->log_cmb_registration("Current year: ".$curyear);
			$this->log_cmb_registration("CMBRegHistoryId: ".$walkRegistration->getCMBRegHistoryId());
			$this->log_cmb_registration("URLKey: ".$urlKey);
			
			$cwr_registrant_home_value = $this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $urlKey, $curyear );
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast( $cwr_registrant_home_value );				

			$numChildren = count($cwr_registrant_home_value->getChildInfo());			
			
			$message = '';
			//$message .= '<p><b>You have Been Registered for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1).'.</b></p><p><b>You do not have to stop at the registration table on opening day.</b></p>';
			$message .= '<p><b>You have Been Registered for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1).'.</b></p>';
			$message .= '<p>Hari OM ' . $walkRegistration->getPerson()->getFirstName() . 'ji,</p>';
			$message .= '<p>Your registration for Chinmaya Mission Boston has been confirmed:</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="150">Username:</td><td>' . $walkRegistration->getPerson()->getUserName() . '</td></tr>';
			//$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($walkRegistration->getPerson()->getPassword()) . '</td></tr>';
			$message .= '<tr><td width="250">Secret question:</td><td>' . $walkRegistration->getPerson()->getSecretQuestion() . '</td></tr>';
			$message .= '<tr><td width="250">Secret answer:</td><td>' . $walkRegistration->getPerson()->getSecretPhrase() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Phone#:</td><td>' . $cwr_registrant_home_value->getPhone1() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Cell#:</td><td>' . $cwr_registrant_home_value->getPhone2() . '</td></tr>';
			if($cwr_registrant_home_value->getSpouseFirstName() != "")
			{
				$message .= '<tr><td width="250">Spouse Name:</td><td>' . $cwr_registrant_home_value->getSpouseFirstName() . ' ' . $cwr_registrant_home_value->getSpouseLastName() . '</td></tr>';				
				$message .= '<tr><td width="250">Spouse Member Email:</td><td>' . $cwr_registrant_home_value->getSpouseEmail1() . '</td></tr>';
				$message .= '<tr><td width="250">Spouse Member Phone#:</td><td>' . $cwr_registrant_home_value->getSpousePhone1() . '</td></tr>';
				$message .= '<tr><td width="250">Spouse Member Cell#:</td><td>' . $cwr_registrant_home_value->getSpousePhone2() . '</td></tr>';
			}
			$message .= '<tr><td width="250">Address:</td><td>' . $walkRegistration->getPerson()->getAddress1() . '</td></tr>';
			$message .= '<tr><td width="250">City:</td><td>' . $walkRegistration->getPerson()->getCity() . '</td></tr>';
			$message .= '<tr><td width="250">State:</td><td>' . $walkRegistration->getPerson()->getState() . '</td></tr>';
			$message .= '<tr><td width="250">Zip Code:</td><td>' . $walkRegistration->getPerson()->getZip() . '</td></tr>';
			$message .= '<tr><td width="250">Email:</td><td>' . $walkRegistration->getPerson()->getEmail1() . '</td></tr>';
			if ($cwr_registrant_home_value->getOrgName() != "")
				$message .= '<tr><td width="250">Company name:</td><td>' . $cwr_registrant_home_value->getOrgName() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Center:</td><td>' . $cwr_registrant_home_value->getCMBCenter() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $walkRegistration->getCMBRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $walkRegistration->getBVRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Donation:</td><td>$' . $walkRegistration->getDonation() . '</td></tr>';
			$message .= '<tr><td width="250">Total:</td><td>$' . $walkRegistration->getTotalRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Paypal Transaction ID:</td><td>' . $paypal_txn_id . '</td></tr>';

			//Child Balavihar information
			if($numChildren > 0)
			{
				$numEnabledChildren = 0;
				$childInfoArray = $cwr_registrant_home_value->getChildInfo();
				for( $i = 0; $i < $numChildren; $i++ )
				{
					$childInfoRes = $childInfoArray[$i];
					if ($childInfoRes[7] == "1")
						$numEnabledChildren ++;
				}
		
				if ($numEnabledChildren > 0)
				{
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"><hr /></td></tr>';
					$message .= '<tr><td width="250"><b><u>Children Balavihar Info:<u><b></td></tr>';
				
					for( $i = 0; $i < $numChildren; $i++ )
					{
						$childInfoRes = $childInfoArray[$i];
						if ($childInfoRes[7] == "1")
						{
							$message .= '<tr><td width="250">Child '.($i+1).': '.$childInfoRes[0] . ' ' . $childInfoRes[1].'</td></tr>';
							$message .= '<tr><td width="250">Gender: '.$childInfoRes[2].'</td></tr>';
							$message .= '<tr><td width="250">DOB: '.substr($childInfoRes[4],0,10).'</td></tr>';
							if ($childInfoRes[3] != "")
								$message .= '<tr><td width="250">Email: '.$childInfoRes[3].'</td></tr>';
							$message .= '<tr><td width="250">Grade: '.$childInfoRes[5].'</td></tr>';
							$message .= '<tr><td width="250">Session: '.$childInfoRes[6].'</td></tr>';
						}
					
						$message .= '<tr><td width="250"><hr /></td></tr>';
					} //for( $i = 0; $i < $numChildren; $i++ )
				} //if ($numEnabledChildren > 0)
			} //if($numChildren > 0)

			//Child Language Info
			if($numChildren > 0)
			{
				$numEnabledChildren = 0;
				$childLangArray = $cwr_registrant_home_value->getChildLang();
				for( $i = 0; $i < $numChildren; $i++ )
				{
					$childInfoRes = $childInfoArray[$i];
					if (($childLangArray[$i] != "None"))
						$numEnabledChildren ++;
				}
		
				if ($numEnabledChildren > 0)
				{
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"><b><u>Children Language Info:<u><b></td></tr>';
				
					$childLangLevelArray = $cwr_registrant_home_value->getChildLangLevel();
					for( $i = 0; $i < $numChildren; $i++ )
					{
						$childInfoRes = $childInfoArray[$i];
						if (($childLangArray[$i] != "None"))
						{
							$message .= '<tr><td width="250">Child '.($i+1).': '.$childInfoRes[0] . ' ' . $childInfoRes[1].'</td></tr>';
							$message .= '<tr><td width="250">Language: '.$childLangArray[$i].'</td></tr>';
							$message .= '<tr><td width="250">Language Level: '.$childLangLevelArray[$i].'</td></tr>';
						}			
						
						$message .= '<tr><td width="250"><hr /></td></tr>';
					} //for( $i = 0; $i < $numChildren; $i++ )
				}
			} //if($numChildren > 0)

			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Login to your home page at : ' . base_url() . '</p>';
			$message .= '<p>You will be contacted with the details of your registration. We would like to thank you for your support.</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( $walkRegistration->getPerson()->getEmail1() );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('Registration confirmation for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
		}
		
		//Confirms registration after check for payment is received
		public function sendMemberRegistrationCheckConfirmEmail( $cwr_registrant_home_value, $curyear, $checknum, $urlKey )
		{
			$this->load->library('encrypt');
			
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($cwr_registrant_home_value);
			$numChildren = count($cwr_registrant_home_value->getChildInfo());
			
			$message = '';
			//$message .= '<p><b>You have Been Registered for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1).'.</b></p><p><b>You do not have to stop at the registration table on opening day.</b></p>';
			$message .= '<p><b>You have Been Registered for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1).'.</b></p>';
			$message .= '<p>Hari OM ' . $cwr_registrant_home_value->getFirstName() . 'ji,</p>';
			$message .= '<p>Your registration for Chinmaya Mission Boston has been confirmed:</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="150">Username:</td><td>' . $cwr_registrant_home_value->getUserName() . '</td></tr>';
			//$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($cwr_registrant_home_value->getPassword()) . '</td></tr>';
			$message .= '<tr><td width="250">Secret question:</td><td>' . $cwr_registrant_home_value->getSecretQuestion() . '</td></tr>';
			$message .= '<tr><td width="250">Secret answer:</td><td>' . $cwr_registrant_home_value->getSecretPhrase() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Phone#:</td><td>' . $cwr_registrant_home_value->getPhone1() . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Cell#:</td><td>' . $cwr_registrant_home_value->getPhone2() . '</td></tr>';
			if($cwr_registrant_home_value->getSpouseFirstName() != "")
			{
				$message .= '<tr><td width="250">Spouse Name:</td><td>' . $cwr_registrant_home_value->getSpouseFirstName() . ' ' . $cwr_registrant_home_value->getSpouseLastName() . '</td></tr>';				
				$message .= '<tr><td width="250">Spouse Member Email:</td><td>' . $cwr_registrant_home_value->getSpouseEmail1() . '</td></tr>';
				$message .= '<tr><td width="250">Spouse Member Phone#:</td><td>' . $cwr_registrant_home_value->getSpousePhone1() . '</td></tr>';
				$message .= '<tr><td width="250">Spouse Member Cell#:</td><td>' . $cwr_registrant_home_value->getSpousePhone2() . '</td></tr>';
			}
			$message .= '<tr><td width="250">Address:</td><td>' . $cwr_registrant_home_value->getAddress1() . '</td></tr>';
			$message .= '<tr><td width="250">City:</td><td>' . $cwr_registrant_home_value->getCity() . '</td></tr>';
			$message .= '<tr><td width="250">State:</td><td>' . $cwr_registrant_home_value->getState() . '</td></tr>';
			$message .= '<tr><td width="250">Zip Code:</td><td>' . $cwr_registrant_home_value->getZip() . '</td></tr>';
			if ($cwr_registrant_home_value->getOrgName() != "")
				$message .= '<tr><td width="250">Company name:</td><td>' . $cwr_registrant_home_value->getOrgName() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Center:</td><td>' . $cwr_registrant_home_value->getCMBCenter() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $cwr_registrant_home_value->getRegAmount() . '</td></tr>';
			$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $cwr_registrant_home_value->getBVAmount() . '</td></tr>';
			$message .= '<tr><td width="250">Donation:</td><td>$' . $cwr_registrant_home_value->getDonation() . '</td></tr>';
			$message .= '<tr><td width="250">Total:</td><td>$' . $cwr_registrant_home_value->getAmountPaid() . '</td></tr>';
			$message .= '<tr><td width="250">Paid By Check#:</td><td>' . $checknum . '</td></tr>';

			//Child Balavihar information
			if($numChildren > 0)
			{
				$numEnabledChildren = 0;
				$childInfoArray = $cwr_registrant_home_value->getChildInfo();
				for( $i = 0; $i < $numChildren; $i++ )
				{
					$childInfoRes = $childInfoArray[$i];
					if ($childInfoRes[7] == "1")
						$numEnabledChildren ++;
				}
		
				if ($numEnabledChildren > 0)
				{
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"><hr /></td></tr>';
					$message .= '<tr><td width="250"><b><u>Children Balavihar Info:<u><b></td></tr>';
				
					for( $i = 0; $i < $numChildren; $i++ )
					{
						$childInfoRes = $childInfoArray[$i];
						if ($childInfoRes[7] == "1")
						{
							$message .= '<tr><td width="250">Child '.($i+1).': '.$childInfoRes[0] . ' ' . $childInfoRes[1].'</td></tr>';
							$message .= '<tr><td width="250">Gender: '.$childInfoRes[2].'</td></tr>';
							$message .= '<tr><td width="250">DOB: '.substr($childInfoRes[4],0,10).'</td></tr>';
							if ($childInfoRes[3] != "")
								$message .= '<tr><td width="250">Email: '.$childInfoRes[3].'</td></tr>';
							$message .= '<tr><td width="250">Grade: '.$childInfoRes[5].'</td></tr>';
							$message .= '<tr><td width="250">Session: '.$childInfoRes[6].'</td></tr>';
						}
					
						$message .= '<tr><td width="250"><hr /></td></tr>';
					} //for( $i = 0; $i < $numChildren; $i++ )
				} //if ($numEnabledChildren > 0)
			} //if($numChildren > 0)

			//Child Language Info
			if($numChildren > 0)
			{
				$numEnabledChildren = 0;
				$childLangArray = $cwr_registrant_home_value->getChildLang();
				for( $i = 0; $i < $numChildren; $i++ )
				{
					$childInfoRes = $childInfoArray[$i];
					if (($childLangArray[$i] != "None"))
						$numEnabledChildren ++;
				}
		
				if ($numEnabledChildren > 0)
				{
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"> </td></tr>';
					$message .= '<tr><td width="250"><b><u>Children Language Info:<u><b></td></tr>';
				
					$childLangLevelArray = $cwr_registrant_home_value->getChildLangLevel();
					for( $i = 0; $i < $numChildren; $i++ )
					{
						$childInfoRes = $childInfoArray[$i];
						if (($childLangArray[$i] != "None"))
						{
							$message .= '<tr><td width="250">Child '.($i+1).': '.$childInfoRes[0] . ' ' . $childInfoRes[1].'</td></tr>';
							$message .= '<tr><td width="250">Language: '.$childLangArray[$i].'</td></tr>';
							$message .= '<tr><td width="250">Language Level: '.$childLangLevelArray[$i].'</td></tr>';
						}			
						
						$message .= '<tr><td width="250"><hr /></td></tr>';
					} //for( $i = 0; $i < $numChildren; $i++ )
				}
			} //if($numChildren > 0)

			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Login to your home page at : ' . base_url() . '</p>';
			$message .= '<p>You will be contacted with the details of your registration. We would like to thank you for your support.</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
						
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( $cwr_registrant_home_value->getEmail1() );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('Registration confirmation by Check for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
			
		}

		//Sends email with link to allow admin to pay by Paypal
		public function sendAdminChangePaymentPaypalEmail( $cwr_registrant_home_value, $curyear, $urlKey, $CMBRegHistoryId )
		{
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($cwr_registrant_home_value);
			
			$message = '';
			$message .= '<p><b>Payment change from check to Paypal for ' . $cwr_registrant_home_value->getFirstName().' for '. $curyear . '-' . ($curyear+1).'</b></p>';
			$message .= '<p>Dear Admin,</p>';
			$message .= '<p>Click the link below to make payment by Paypal:</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="250">Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
			$message .= '<tr><td width="250">Address:</td><td>' . $cwr_registrant_home_value->getAddress1() . '</td></tr>';
			$message .= '<tr><td width="250">City:</td><td>' . $cwr_registrant_home_value->getCity() . '</td></tr>';
			$message .= '<tr><td width="250">State:</td><td>' . $cwr_registrant_home_value->getState() . '</td></tr>';
			$message .= '<tr><td width="250">Zip Code:</td><td>' . $cwr_registrant_home_value->getZip() . '</td></tr>';
			$message .= '<tr><td width="250">Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
			$message .= '<tr><td width="250">Company name:</td><td>' . $cwr_registrant_home_value->getOrgName() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $cwr_registrant_home_value->getRegAmount() . '</td></tr>';
			$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $cwr_registrant_home_value->getBVAmount() . '</td></tr>';
			$message .= '<tr><td width="250">Donation:</td><td>$' . $cwr_registrant_home_value->getDonation() . '</td></tr>';
			$message .= '<tr><td width="250">Total:</td><td>$' . $cwr_registrant_home_value->getAmountPaid() . '</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Pay by Paypal : ' . base_url() . 'cmb/member_registration/change_payment_paypal/'.$urlKey.'/'.$CMBRegHistoryId.'/</p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( 'cmbproject@gmail.com' );
			$this->email->cc('');
			$this->email->bcc('');
			
			$this->email->subject('Change Payment For Registered Member to Paypal for ' . $curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
		}

		//Acknowledges registration request
		public function sendMemberRegistrationRequestEmail( $walkRegistration, $curyear )
		{
			$this->load->library('encrypt');
			try
			{						
				$walkRegistration = Member_registration_value::cast($walkRegistration);
				
				$message = '';
				$message .= '<p>Hari OM ' . $walkRegistration->getPerson()->getFirstName() . 'ji,</p>';
				$message .= '<p>We have received your registration request for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1) . '.</p>';
				$message .= '<p>';
				$message .= '<table border="0">';
				//$message .= '<tr><td width="150">Username:</td><td>' . $walkRegistration->getPerson()->getUserName() . '</td></tr>';
				//$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($walkRegistration->getPerson()->getPassword()) . '</td></tr>';
				$message .= '<tr><td width="250">Name:</td><td>' . $walkRegistration->getPerson()->getFirstName() . ' ' . $walkRegistration->getPerson()->getLastName() . '</td></tr>';
				$message .= '<tr><td width="250">Address:</td><td>' . $walkRegistration->getPerson()->getAddress1() . '</td></tr>';
				$message .= '<tr><td width="250">City:</td><td>' . $walkRegistration->getPerson()->getCity() . '</td></tr>';
				$message .= '<tr><td width="250">State:</td><td>' . $walkRegistration->getPerson()->getState() . '</td></tr>';
				$message .= '<tr><td width="250">Zip Code:</td><td>' . $walkRegistration->getPerson()->getZip() . '</td></tr>';
				$message .= '<tr><td width="250">Email:</td><td>' . $walkRegistration->getPerson()->getEmail1() . '</td></tr>';
				$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $walkRegistration->getCMBRegistrationFee() . '</td></tr>';
				$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $walkRegistration->getBVRegistrationFee() . '</td></tr>';
				$message .= '<tr><td width="250">Donation:</td><td>$' . $walkRegistration->getDonation() . '</td></tr>';
				$message .= '<tr><td width="250">Total:</td><td>$' . $walkRegistration->getTotalRegistrationFee() . '</td></tr>';
				$message .= '</table>';
				$message .= '</p>';
				$message .= '<p>You will receive a registration confirmation email as soon as your payment has been approved. We would like to thank you for your support.</p>';
				$message .= '<p>Sincerely,</p>';
				$message .= '<p>Registration Team</p>';
				$message .= '<p>Chinmaya Mission Boston<br /></p>';
				
				$this->load->library('email');
				$this->email->set_newline("\r\n");
				
				$config['protocol'] = $this->config->item('email_smtp_protocol');
				$config['smtp_host'] = $this->config->item('email_smtp_host');
				$config['smtp_user'] = $this->config->item('email_smtp_user');
				$config['smtp_pass'] = $this->config->item('email_smtp_pass');
				$config['smtp_port'] = $this->config->item('email_smtp_port');
				$config['mailtype'] = $this->config->item('email_smtp_mailtype');
		
				$this->email->initialize($config);
		
				$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
				$this->email->to( $walkRegistration->getPerson()->getEmail1() );
				$this->email->cc('');
				$this->email->bcc('cmbproject@gmail.com');
				
				$this->email->subject('Received Registration Request for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1));
				$this->email->message($message);
/*				
				echo "SMTP Host: ".$this->config->item('email_smtp_host')."<br/>";
				echo "SMTP User: ".$this->config->item('email_smtp_user')."<br/>";
				echo "SMTP User Name: ".$this->config->item('email_smtp_user_name')."<br/>";
				echo "SMTP Password: ".$this->config->item('email_smtp_pass')."<br/>";
				echo "SMTP Port: ".$this->config->item('email_smtp_port')."<br/>";
				echo "SMTP Mail Type: ".$this->config->item('email_smtp_mailtype')."<br/>";
				echo "To: ".$walkRegistration->getPerson()->getEmail1()."<br/>";
				
				echo $message;
				die;			
*/
				$this->email->send();
			} catch (Exception $e)
			{
				echo 'Email exception: ',  $e->getMessage(), "\n";
				die;
			}
		}
		
		//Notification to Admin that person chose payment by check
		public function sendMemberRegistrationByCheckAdminEmail( $walkRegistration, $curyear )
		{
			$this->load->library('encrypt');
						
			$walkRegistration = Member_registration_value::cast($walkRegistration);
			
			$message = '';
			$message .= '<p>Member ' . $walkRegistration->getPerson()->getFirstName() . ' ' . $walkRegistration->getPerson()->getLastName() . ' is requesting registration with payment by CHECK:</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			//$message .= '<tr><td width="150">Username:</td><td>' . $walkRegistration->getPerson()->getUserName() . '</td></tr>';
			//$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($walkRegistration->getPerson()->getPassword()) . '</td></tr>';
			$message .= '<tr><td width="250">Primary Member Person ID:</td><td>' . $walkRegistration->getPerson()->getPersonId() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration History ID:</td><td>' . $walkRegistration->getCMBRegHistoryId() . '</td></tr>';
			$message .= '<tr><td width="250">Name:</td><td>' . $walkRegistration->getPerson()->getFirstName() . ' ' . $walkRegistration->getPerson()->getLastName() . '</td></tr>';
			$message .= '<tr><td width="250">Address:</td><td>' . $walkRegistration->getPerson()->getAddress1() . '</td></tr>';
			$message .= '<tr><td width="250">City:</td><td>' . $walkRegistration->getPerson()->getCity() . '</td></tr>';
			$message .= '<tr><td width="250">State:</td><td>' . $walkRegistration->getPerson()->getState() . '</td></tr>';
			$message .= '<tr><td width="250">Zip Code:</td><td>' . $walkRegistration->getPerson()->getZip() . '</td></tr>';
			$message .= '<tr><td width="250">Email:</td><td>' . $walkRegistration->getPerson()->getEmail1() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $walkRegistration->getCMBRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $walkRegistration->getBVRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Donation:</td><td>$' . $walkRegistration->getDonation() . '</td></tr>';
			$message .= '<tr><td width="250">Total:</td><td>$' . $walkRegistration->getTotalRegistrationFee() . '</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( 'cmbproject@gmail.com' );
			$this->email->cc('');
			
			$this->email->subject('Registration Request by CHECK for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
		}	
		
		//EMail with information about sending check after person has chosen to pay by check
		public function sendMemberRegistrationByCheckEmail( $walkRegistration, $curyear )
		{
			$this->load->library('encrypt');
						
			$walkRegistration = Member_registration_value::cast($walkRegistration);
			
			$message = '';
			$message .= '<p>Hari OM ' . $walkRegistration->getPerson()->getFirstName() . 'ji,</p>';
			$message .= '<p>We received your registration request with payment by <u>check</u> for ' . $curyear . '-' . ($curyear+1) . ':</p>';
			$message .= '<p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="250">Name:</td><td>' . $walkRegistration->getPerson()->getFirstName() . ' ' . $walkRegistration->getPerson()->getLastName() . '</td></tr>';
			$message .= '<tr><td width="250">Address:</td><td>' . $walkRegistration->getPerson()->getAddress1() . '</td></tr>';
			$message .= '<tr><td width="250">City:</td><td>' . $walkRegistration->getPerson()->getCity() . '</td></tr>';
			$message .= '<tr><td width="250">State:</td><td>' . $walkRegistration->getPerson()->getState() . '</td></tr>';
			$message .= '<tr><td width="250">Zip Code:</td><td>' . $walkRegistration->getPerson()->getZip() . '</td></tr>';
			$message .= '<tr><td width="250">Email:</td><td>' . $walkRegistration->getPerson()->getEmail1() . '</td></tr>';
			$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $walkRegistration->getCMBRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $walkRegistration->getBVRegistrationFee() . '</td></tr>';
			$message .= '<tr><td width="250">Donation:</td><td>$' . $walkRegistration->getDonation() . '</td></tr>';
			$message .= '<tr><td width="250">Total:</td><td>$' . $walkRegistration->getTotalRegistrationFee() . '</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Please write a check for <b>$' . $walkRegistration->getTotalRegistrationFee() . '</b>, payable to <u>Chinmaya Mission Boston</u>. Mail the check to:</p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="250"></td><td>ATTN: Member Registration</td></tr>';
			$message .= '<tr><td width="250"></td><td>Chinmaya Mission Boston</td></tr>';
			$message .= '<tr><td width="250"></td><td>1 Union Street</td></tr>';
			$message .= '<tr><td width="250"></td><td>Andover, MA  01810</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>You will receive a confirmation email once we receive your check. We would like to thank you for your support.</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( 'cmbproject@gmail.com' );
			$this->email->cc('');
			
			$this->email->subject('Chinmaya Mission Boston registration with payment by check for ' . $curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
		}	
	}
?>